package com.icecola.d8_thread_pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
    1.临时线程什么时候创建 corePoolSize + workQueue 都满，招临时工
        新任务提交时发现核心线程都在忙，任务队列也满了，并且还可以创建临时线程
    2.什么时候开始拒绝新任务 maximumPoolSize + workQueue 都满，拒绝任务
        核心线程和临时线程都满，任务队列也满，新的任务过来会开始拒绝任务
 */
// 通过ThreadPoolExecutor创建线程对象
public class ThreadPoolTest1 {
    public static void main(String[] args) {

        // public ThreadPoolExecutor(int corePoolSize,
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        Runnable target = new MyRunnable();
        //自动创建新线程，自动处理
        threadPoolExecutor.execute(target);
        threadPoolExecutor.execute(target);
        threadPoolExecutor.execute(target);
        //任务队列中 ———— 复用前面的核心线程
        threadPoolExecutor.execute(target);
        threadPoolExecutor.execute(target);
        threadPoolExecutor.execute(target);
        threadPoolExecutor.execute(target);

        //等线程池任务执行完毕后停止
        threadPoolExecutor.shutdown();
        //立即关闭线程池，不管任务是否执行完毕
        threadPoolExecutor.shutdownNow();

    }
}
